<?php

class Application_Model_Dao_Studies extends Zend_Db_Table_Abstract
{

	protected $_name = 'studies';
    protected $_primary = 'id';
    
	public function init(){
    	
    }
    public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
			throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");	
		return $result;
    }   
public function getAllDegreeStudy(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('studies');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
public function getRowDegreeStudy($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('studies')->where('degree_studies.id = \''.$id.'\'');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	public function getDegreeStudyByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
			throw new Exception("Stopień studiów o ID $id nie istnieje.");
		return $row->toArray();
	}
	public function getADegreeStudyByDesignation($designation){
		$row = $this->fetchRow('designation = '.$designation);
		if (!$row)
			throw new Exception("Stopień studiów o nazwie $designation nie istnieje.");
		return $row->toArray();
	}
  	public function saveDegreeStudy($degreeStudy) {
    	$data = array(
			'designation' => $degreeStudy->getDesignation()
		);
		$this->insert($data);
     return ;
    }
	public function updateDegreeStudy($degreeStudy) {
		$data = array(
			'designation' => $degreeStudy->getDesignation(),
		);
		$this->update($data, 'id = '. (int)$degreeStudy->getId());
		return ;
	}
public function deleteDegreeStudy($degreeStudy){
		$this->delete('id =' . (int)$degreeStudy->getId());
		return;
	}
	
  	public function getToSelect(){
    try {	
			$db = Zend_Db_Table::getDefaultAdapter();
			$sql = 'SELECT s.id as id, t.designation as studies_type, m.designation as studies_mode, d.designation as studies_degree 
				FROM studies as s
				INNER JOIN studies_types as t
				INNER JOIN studies_modes as m
				INNER JOIN studies_degrees as d';
    		$stmt = $db->query($sql);
		} catch (Exception $exception) {
				Zend_Registry::get('logger')->log('Brak wyników. Exception: '.$exception, Zend_Log::NOTICE);
		}
		
    	$users[''] = "wybierz";
    	
    	while ($row = $stmt->fetch()) {
     		$users[ $row['id'] ] = $row['studies_type']." ".$row['studies_mode']." ".$row['studies_degree'] ; 
     	}
     return $users;
    }
        
}
   